package com.ksyzt.ksite2.client.news;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.ksyzt.gwt.client.common.MessageComposite;
import com.ksyzt.gwt.client.common.PageNaviBar;
import com.ksyzt.gwt.client.common.SimpleMessage;
import com.ksyzt.gwt.client.event.MessageEvent;
import com.ksyzt.gwt.client.event.MessageHandler;
import com.ksyzt.ksite2.client.common.ServerProxy;
import com.ksyzt.ksite2.shared.PageNewsData;
import com.ksyzt.ksite2.shared.SysConst;
import com.ksyzt.ksite2.shared.modle.COLUMNObj;
import com.ksyzt.ksite2.shared.modle.VW_NEWSObj;

public class PictureList extends MessageComposite {

	private static PictureListUiBinder uiBinder = GWT
			.create(PictureListUiBinder.class);

	interface PictureListUiBinder extends UiBinder<Widget, PictureList> {
	}

	private MessageHandler m_navi_handler = new MessageHandler() {

		@Override
		public void onMessage(Object sender, Integer message, Object value) {
			if (message == MessageEvent.PAGE) {
				PageNaviBar.NaviData data = (PageNaviBar.NaviData) value;
				ServerProxy.SYS_MANAGER.getNews(m_column.getID(), data.page,
						data.size, m_on_data);
			}

		}
	};

	public PictureList() {
		initWidget(uiBinder.createAndBindUi(this));
		navi.addMessageHandler(m_navi_handler);
	}

	COLUMNObj m_column;
	PageNewsData m_data;
	private AsyncCallback<PageNewsData> m_on_data = new AsyncCallback<PageNewsData>() {

		public void onSuccess(PageNewsData result) {
			m_data = result;
			list();
		}

		public void onFailure(Throwable caught) {
			message(caught.getMessage());

		}
	};



	public void initList(COLUMNObj column) {
		m_column = column;
		lbOrgName.setText(column.getNAME());
		ServerProxy.SYS_MANAGER.getNews(m_column.getID(), 0, 20, m_on_data);

	}

	@UiHandler("btnRefresh")
	void refresh(ClickEvent e) {
		MessageEvent ev = new MessageEvent(MessageEvent.REFRESH, null);
		fireEvent(ev);
	}

	int m_userid;

	public void setUserId(int userid) {
		m_userid = userid;

	}

	@UiField
	Label lbOrgName;
	@UiField
	PageNaviBar navi;

	@UiField
	FlowPanel m_list;

	void list() {
		m_list.clear();

		if (m_data != null) {
			navi.setData(m_data.currentPage, (int) m_data.total,
					m_data.pagesize);
			String width = "250px";
			GWT.log("list picture" + m_data.news.size());

			for (VW_NEWSObj n : m_data.news) {
				PictureBox box = new PictureBox();
				box.renderPicture(n);
				m_list.add(box);
			}

			if (m_data.news.size() == 0) {
				SimpleMessage m = new SimpleMessage(SysConst.MSG_NO_RESULT);
				m_list.add(m);
			}
		}
	}

	DialogBox m_dlg;
	UploadPictures uploader;

	@UiHandler("btnCreate")
	void create(ClickEvent e) {
		sureDialog();
		m_dlg.show();
		m_dlg.center();

		uploader.init(m_column.getID() + "");
	}

	
	/* (non-Javadoc)
	 * @see com.google.gwt.user.client.ui.Composite#onDetach()
	 */
	@Override
	protected void onDetach() {
		
		super.onDetach();
		PictureBox.hideEditor();
	}

	private void sureDialog() {
		if (m_dlg == null) {
			m_dlg = new DialogBox();
			m_dlg.setModal(true);
			m_dlg.setGlassEnabled(true);
			m_dlg.setHTML("上传图片");
			uploader = new UploadPictures();
			m_dlg.setWidget(uploader);
			uploader.addMessageHandler(m_editor_handler);
		}
	}

	private MessageHandler m_editor_handler = new MessageHandler() {

		@Override
		public void onMessage(Object sender, Integer message, Object value) {
			if (message == MessageEvent.OK) {

				initList(m_column);
				m_dlg.hide();

			} else if (message == MessageEvent.CANCEL) {
				m_dlg.hide();
			}
		}
	};

}
